<template>
  <div class="outside">
    <div class="app-container" style="width: 92%">
      <inline-form
        ref="InlineForm"
        :item="item"
        @doSubmit="handleDoSubmit"
      ></inline-form>
    </div>
  </div>
</template>

<script>
import _form from "./_form";
import { queryItem, edit } from "./api/index";
import { addImage } from "../../utils/common";

export default {
  data() {
    return {
      id: null,
      item: {
        title: "",
        body: "",
        mobile: "",
        createTime: "",
        topTime: "",
        views: "",
        deleted: "",
        isTop: "",
        isHot: "",
        images: "",
      },
      updateItemFunc: (target) => {
        this.item = {
          title: target.title,
          body: target.body,
          mobile: target.mobile,
          createTime: target.createTime,
          topTime: target.topTime,
          views: target.views,
          deleted: target.deleted+'',
          isTop: target.isTop+'',
          isHot: target.isHot+'',
          images: target.images,
          schoolId:target.schoolId,
        };
      },
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.get_item();
  },
  methods: {
    get_item() {
      if (this.id) {
        // 查
        queryItem(this.id).then((data) => {
          console.log(data);
          if (data.code === 1) {
            let act = data.data;
            this.updateItemFunc(act);
            console.log(this.item);
          } else {
            this.$message.error(data.msg);
          }
        });
      } else {
        $this.$message.error("数据ID不存在，请检查后重写");
      }
    },
    // 修改
    async handleDoSubmit() {
      let form = this.$refs.InlineForm;
      form.valid(async (valid) => {
        if (valid) {
          let item = form.obj;
          console.log(item, "修改");
          if (
            item.imageFile &&
            item.imageFile !== "" &&
            item.imageFile !== "img_none"
          ) {
            item.image = await addImage(item.imageFile);
          }
          delete item.imageFile;
          const res = await edit({ id: this.id, ...item });
          console.log(res, "====>");
          if (res.code === 1) {
            window.sessionStorage.setItem("schoolId", item.schoolId);
            this.$message.success("修改成功!");
            this.$router.go(-1);
          } else {
            this.$message.error(res.msg);
          }
        } else {
          this.$message.error("数据填写错误，请按要求填写!");
        }
      });
    },
  },
  components: {
    "inline-form": _form,
  },
};
</script>

<style scoped>
</style>
